package com.cndw.kungfu.comm;

import java.net.URI;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.MD5;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.openqq.client.ClientRequest;
import com.cndw.openqq.client.ClientURI;

/**
 * 交叉推广的任务编号
 * 
 * @author fantadust
 * 
 */
public class JXSpreadTask {

	/**
	 * 升级到10级
	 */
	public static final int level10 = 1;

	public static final int level20 = 2;

	public static final int level30 = 4;
	/**
	 * 邀请好友
	 */
	public static final int invite = 8;
	/**
	 * 加入帮派
	 */
	public static final int applyUnion = 16;
	/**
	 * 招聘一次徒弟
	 */
	public static final int reinDis = 32;
	/**
	 * 进行一次武林大会
	 */
	public static final int pvp = 64;

	public static void complete(final Player player, int type) {

		int jx = player.getDataInfo().intDataGet(ConstantsIntKey.jx);
		if (jx > 0 && (jx & type) > 0) {
			int jxe = player.getDataInfo().intDataGet(ConstantsIntKey.jxe);
			if ((jxe & type) <= 0) {
				jxe |= type;
				player.getDataInfo().intDataSet(ConstantsIntKey.jxe, jxe);
				if (jxe == jx) {
					Context.getTimerServer().execute(new Runnable() {

						@Override
						public void run() {

							sendUrl(player);

						}
					});
				}
			}

		}
	}

	private static void sendUrl(Player player) {
		String act = "taskOk";
		// Data
		StringBuilder data = new StringBuilder();
		data.append(ConstantsConfig.getSysCgCrossURL());
		data.append("opencomm?action=taskCompleted");
		data.append("&act=" + act);
		data.append("&openId=").append(player.getAccountName());
		data.append("&openKey=").append(player.getDataInfo().getOpenKey());
		data.append("&pf=").append(player.getDataInfo().getPf());
		data.append("&pfKey=").append(player.getDataInfo().getPfKey());
		data.append("&svrId=").append(ConstantsConfig.getSysCgSvrId());
		data.append("&userip=").append(player.getLastIp());
		data.append("&sign=");
		data.append(MD5.compile(player.getAccountName().concat(act).concat(ConstantsConfig.getSysCgKeyPlatform())));
		data.append("&ts=").append(TimeProcesser.getUnixTime());
		data.append("&s=").append(Math.random());

		URI uri = ClientURI.createURI(data.toString());

		
		try {
			ClientRequest.sendGetRequest(uri, 3000, 5000);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}

	}

}
